package com.meishij.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.meishij.R;
import com.meishij.adapter.ShiHua_Detail_Adapter;
import com.meishij.entry.ShiHua_Datail_Entry;
import com.meishij.server.ShiHuaServer;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShiHua_Detail_Activity extends AppCompatActivity implements Callback<ShiHua_Datail_Entry> {

    private String tid;
    private String gid;
    private RecyclerView recyclerView;
    private ShiHuaServer shiHuaServer;
    private List<Object> list;
    private static final String TAG = ShiHua_Detail_Activity.class.getSimpleName();
    private ShiHua_Detail_Adapter shiHua_detail_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shihua_detail);
        Intent intent = getIntent();
        tid = intent.getStringExtra("tid");
        gid = intent.getStringExtra("gid");
        recyclerView = ((RecyclerView) findViewById(R.id.recycler_shihua_detail));
        list = new ArrayList<>();
        Retrofit build = new Retrofit.Builder()
                .baseUrl("http://api.meishi.cc/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        shiHuaServer = build.create(ShiHuaServer.class);
        Call<ShiHua_Datail_Entry> aNew = shiHuaServer.getShiHuaTopicEntry("new", gid, "android", tid, "1", "json");
        aNew.enqueue(this);
    }

    @Override
    public void onResponse(Call<ShiHua_Datail_Entry> call, Response<ShiHua_Datail_Entry> response) {
        list.clear();
        ShiHua_Datail_Entry body = response.body();
        list.add(0, body.getTopic());
        list.add(1, body.getComment_list());
        LinearLayoutManager layoutManager = new LinearLayoutManager(ShiHua_Detail_Activity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        shiHua_detail_adapter = new ShiHua_Detail_Adapter(this, list, new int[]{
                R.layout.item_shihua_detail_topic, R.layout.item_shihua_detail_comment});
        recyclerView.setAdapter(shiHua_detail_adapter);
    }

    @Override
    public void onFailure(Call<ShiHua_Datail_Entry> call, Throwable t) {
        Toast.makeText(this, "吃的太撑了 稍后再试吧" + t.getMessage(), Toast.LENGTH_SHORT).show();
        Log.e(TAG, "onFailure" + t.getMessage());
    }
}
